package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.vo.BrandCategoryStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.IBrandCategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BrandCategoryServiceImpl implements IBrandCategoryService {
    @Autowired
    private BrandCategoryMapper brandCategoryMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    public BrandCategoryServiceImpl() {
        log.debug("创建业务类对象：BrandCategoryServiceImpl");
    }

    @Override
    public void insert(Long brandId, Long categoryId) {
        log.debug("开始处理【绑定品牌与类别的关联】的业务，品牌id：{}，类别id：{}", brandId, categoryId);
        BrandStandardVO brandQueryResult = brandMapper.getStandardById(brandId);
        if (brandQueryResult == null){
            String message = "绑定品牌与类别的关联失败，尝试绑定的品牌不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        if (brandQueryResult.getEnable() == 0){
            String message = "绑定品牌与类别的关联失败，尝试绑定的品牌已被禁用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        CategoryStandardVO categoryQueryResult = categoryMapper.getStandardById(categoryId);
        if (categoryQueryResult == null){
            String message = "绑定品牌与类别的关联失败，尝试绑定的类别不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        if (categoryQueryResult.getEnable() == 0){
            String message = "绑定品牌与类别的关联失败，尝试绑定的类别已被禁用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        BrandCategoryStandardVO queryResult = brandCategoryMapper.getStandardByBrandIdAndCategoryId(brandId, categoryId);
        if (queryResult!=null){
            String message = "绑定品牌与类别的关联失败，尝试绑定的品牌与类别已关联！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        BrandCategory brandCategory = new BrandCategory();
        brandCategory.setBrandId(brandId);
        brandCategory.setCategoryId(categoryId);
        log.debug("即将开始插入数据");
        int rows = brandCategoryMapper.insert(brandCategory);
        if (rows != 1){
            String message = "绑定品牌与类别的关联失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【解绑品牌与类别的关联】的业务，参数id：{}", id);
        Object queryResult = brandCategoryMapper.getStandardById(id);
        if (queryResult == null){
            String message = "解绑品牌与类别的关联失败，尝试解绑的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.debug("即将执行删除数据，参数：{}",id);
        int rows = brandCategoryMapper.deleteById(id);
        if (rows != 1){
            String message = "解绑品牌与类别的关联失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }
}
